package common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class BaseDAO {
	/**
	 * 获取连接池
	 * @return
	 */
	public Connection getConnection(){
		Context ctx;
		DataSource ds;
		try {
			//加载配置文件,实例化一个Context对象
			ctx = new InitialContext();
			// 通过在context.xml文件设定的数据源对象的名字，获取数据源对象
			//vmall是WebContent/META-INF/context.xml的name值
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/vmall");
			return ds.getConnection();
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 关闭连接
	 * @param conn
	 * @param stmt
	 * @param rst
	 */
	public void close(Connection conn,Statement stmt,ResultSet rst)
	{
		//第六步  关闭连接池
		try {
			if(rst!=null&&!rst.isClosed()){
			 rst.close();
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			if(stmt!=null&&!stmt.isClosed()){
			 stmt.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
	    	if(conn!=null&&!conn.isClosed())
	    	{
			 conn.close();
	    	}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 绑定参数
	 * @param stmt
	 * @param params
	 * @throws SQLException
	 */
	private void bindParameters(PreparedStatement stmt, Object... params) throws SQLException {
		for(int i = 0; i < params.length; i ++){
			stmt.setObject(i + 1, params[i]);
		}
	}
	
	
	/**
	 * 新的执行方式 增 删 改
	 * */
	public void update(String sql,Object ...params)
	{
		Connection conn=getConnection();
		PreparedStatement stmt = null;
		 //执行sql语句
		try {
			stmt = conn.prepareStatement(sql);
			
			if(params != null){
				bindParameters(stmt, params);
			}
			stmt.executeUpdate();
		}
        catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			close(conn,stmt,null);
		}
	}

	
	/**
	 * 查询
	 * @param sql
	 * @param rowMapper
	 * @param params
	 * @return
	 */
	public <T> List<T> query(String sql,RowMapper<T> rowMapper,Object... params){
		 Connection conn=getConnection();
		 PreparedStatement  stmt=null;
		 ResultSet rst=null;
		  try {
			stmt=conn.prepareStatement(sql);
			if(params != null){
				bindParameters(stmt, params);
			}
			  rst=stmt.executeQuery();
			  if(rst != null){
					List<T> list = new ArrayList<T>();
					while(rst.next()){
						T t = rowMapper.mapper(rst);
						list.add(t);
					}
					return list;
				}
		} 
		 //ResultSet 来接收查询到的结果集
		  catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 finally
		  {
			  close(conn, stmt, rst);
		  }
		 return null;
	}
	
	public int getCount(String sql,Object... params){
		 Connection conn=getConnection();
		 PreparedStatement  stmt=null;
		 ResultSet rst=null;
		  try {
			stmt=conn.prepareStatement(sql);
			if(params != null){
				bindParameters(stmt, params);
			}
			  rst=stmt.executeQuery();
			  if(rst != null){
				  while(rst.next()){
					  return rst.getInt("count(*)");
				  }
				  
			  }
	}
		  catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 finally
		  {
			  close(conn, stmt, rst);
		  }
		 return 0;
	}
}
